package com.example.costumview.customview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.costumview.R;


public class AliPayView extends View {
    private static final String TAG = "GetPosTanView";
    private Path mCirclePath, mDstPath;
    private Paint mPaint;
    private PathMeasure mPathMeasure;
    private Float mCurAnimValue;


    private float mCentX=200,mCentY=200,mRadius=150;

    public AliPayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        mPaint=new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(8);

        mCirclePath = new Path();
        mDstPath = new Path();

//        mCirclePath.addCircle(getWidth()/2-100,getWidth()/2-100,getWidth()/2-300, Path.Direction.CW);
        mCirclePath.addCircle(mCentX,mCentY,mRadius, Path.Direction.CW);

        mCirclePath.moveTo(mCentX-mRadius/2,mCentY);
        mCirclePath.lineTo(mCentX,mCentY+mRadius/2);
        mCirclePath.lineTo(mCentX+mRadius/2,mCentY-mRadius/3);

        mPathMeasure = new PathMeasure(mCirclePath,false);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 2);
        valueAnimator.addUpdateListener(animation -> {
            mCurAnimValue = (Float) animation.getAnimatedValue();
            invalidate();
        });
        valueAnimator.setDuration(4000);
        valueAnimator.start();
    }

    private boolean mNext=false;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);

        if (mCurAnimValue<1){
            float stop = mPathMeasure.getLength() * mCurAnimValue;
            mPathMeasure.getSegment(0,stop,mDstPath,true);
        }else{
            if (!mNext){
                mNext=true;
                mPathMeasure.getSegment(0,mPathMeasure.getLength(),mDstPath,true);
                mPathMeasure.nextContour();
            }
            float stop = mPathMeasure.getLength() * (mCurAnimValue - 1);
            mPathMeasure.getSegment(0,stop,mDstPath,true);
        }

        canvas.drawPath(mDstPath,mPaint);

    }
}
